﻿using System;
using System.IO;
using System.Windows.Forms;
using Controller;

namespace Reversi
{
	public partial class UCStart : UserControl
	{
		private PlayerTypes player1;
		private PlayerTypes player2;

		private int timerInterval;
		private const string initialFilename = "Konfiguration/NeuralNet.xml";
		private string configFilename1;
		private string configFilename2;

		public UCStart(PlayerTypes p1 = PlayerTypes.Player, PlayerTypes p2 = PlayerTypes.Computer, string settingsNeuralNet1 = initialFilename, string settingsNeuralNet2 = initialFilename, int timerInterval = 1500)
		{
			this.player1 = p1;
			this.player2 = p2;
			this.timerInterval = timerInterval;
			this.configFilename1 = settingsNeuralNet1;
			this.configFilename2 = settingsNeuralNet2;
			InitializeComponent();
			openFileDialog1.FileName = txtBxPlayer1.Text;
			openFileDialog1.Filter = "XML (*.xml)|*.xml";
		}

		#region Events

		private void UCStart_Load(object sender, EventArgs e)
		{
			string[] tmpPlayers = Enum.GetNames(typeof(PlayerTypes));
			cmBxPlayer1.Items.AddRange(tmpPlayers);
			cmBxPlayer1.SelectedItem = player1.ToString();
			cmBxPlayer2.Items.AddRange(tmpPlayers);
			cmBxPlayer2.SelectedItem = player2.ToString();

			InitConfig(configFilename1, txtBxPlayer1);
			InitConfig(configFilename2, txtBxPlayer2);

			nUDTimerInterval.Value = timerInterval;
			ChangeUI(player1, pnlPlayer1);
			ChangeUI(player2, pnlPlayer2);
		}

		private void InitConfig(string configFilename, TextBox txtBxPlayer)
		{
			if (File.Exists(configFilename))
			{
				FileInfo fi = new FileInfo(configFilename);
				openFileDialog1.FileName = fi.Name;
				openFileDialog1.InitialDirectory = fi.DirectoryName;
				txtBxPlayer.Text = fi.FullName;
			}
		}

		private void cmBxPlayer1_SelectedIndexChanged(object sender, EventArgs e)
		{
			player1 = (PlayerTypes)Enum.Parse(typeof(PlayerTypes), cmBxPlayer1.SelectedItem.ToString());
			ChangeUI(player1, pnlPlayer1);
		}

		private void cmBxPlayer2_SelectedIndexChanged(object sender, EventArgs e)
		{
			player2 = (PlayerTypes)Enum.Parse(typeof(PlayerTypes), cmBxPlayer2.SelectedItem.ToString());
			ChangeUI(player2, pnlPlayer2);
		}

		private void btnOpen_Click(object sender, EventArgs e)
		{
			TextBox txtBx = sender == btnOpen1 ? txtBxPlayer1 : txtBxPlayer2;
			if (openFileDialog1.ShowDialog() == DialogResult.OK)
			{
				txtBx.Text = openFileDialog1.FileName;
			}
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			if (player1 == PlayerTypes.NN && txtBxPlayer1.Text == string.Empty)
			{
				ErrorHandler.ShowInformation("Bitte geben Sie eine Konfigurationsdatei für Spieler 1 an.", "Fehlende Konfigurationsdatei für Neuronales Netz");
				return;
			}
			if (player2 == PlayerTypes.NN && txtBxPlayer2.Text == string.Empty)
			{
				ErrorHandler.ShowInformation("Bitte geben Sie eine Konfigurationsdatei für Spieler 2 an.", "Fehlende Konfigurationsdatei für Neuronales Netz");
				return;
			}
			OnSettingsChanged();
		}

		#endregion

		#region Work

		private void ChangeUI(PlayerTypes player, Panel panel)
		{
			if (player == PlayerTypes.NN)
			{
				if (!this.Controls.Contains(panel))
				{
					this.Controls.Add(panel);
				}
			}
			else
			{
				if (this.Controls.Contains(panel))
				{
					this.Controls.Remove(panel);
				}
			}
		}

		public delegate void SettingsChangedEventHandler(object sender, SettingsChangedEventArgs e);

		public event SettingsChangedEventHandler SettingsChanged;

		protected void OnSettingsChanged()
		{
			if (SettingsChanged != null)
			{
				SettingsChangedEventArgs args = new SettingsChangedEventArgs();
				args.Player1 = player1;
				args.Player2 = player2;
				args.TimerInterval = (int)nUDTimerInterval.Value;
				args.SettingsNeuralNet1 = txtBxPlayer1.Text;
				args.SettingsNeuralNet2 = txtBxPlayer2.Text;

				SettingsChanged(this, args);
			}
		}

		#endregion

	}
}
